<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Admin;


use Srv\Apps\Apis\Admin\Ctl\AuthCtl;
use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Proto\DTO\Auth\FrontRoleAddDTO;
use Srv\Apps\Proto\DTO\Auth\FrontRoleEditDTO;
use Srv\Apps\Proto\DTO\Auth\FrontRoleInfoDTO;
use Srv\Apps\Proto\DTO\Auth\FrontRoleInfoListDTO;
use Srv\Apps\Proto\DTO\Auth\FrontRouteAsyncDTO;
use Srv\Apps\Proto\DTO\Auth\FrontRouteListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Auth\FrontRoleInfoListVO;
use Srv\Apps\Proto\VO\Auth\FrontRoleInfoVO;
use Srv\Apps\Proto\VO\Auth\FrontRouteListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class AuthAct extends ActionAbstract
{
    /**
     * @url /front/route/list
     * @auth frontReqList
     * @flag REQ_LOG=0
     * @req Srv.Apps.Proto.DTO.Auth.FrontRouteListDTO
     * @rsp Srv.Apps.Proto.VO.Auth.FrontRouteListVO
     * 前端路由列表
     */
    public function getFrontRouteList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass): bool
    {
        if (!($DTOClass instanceof FrontRouteListDTO) || !($VOClass instanceof FrontRouteListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page                   = new Page();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = AuthCtl::tag()->getFrontRouteList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /front/route/list/async
     * @auth frontRouteListAsync
     * @req Srv.Apps.Proto.DTO.Auth.FrontRouteAsyncDTO
     * 前端路由同步
     */
    public function reqInfoListAsync(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass): bool
    {
        unset($VOClass);
        if (!($DTOClass instanceof FrontRouteAsyncDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $result         = AuthCtl::tag()->reqInfoListAsync($setData);
        $LogCtl         = LogCtl::tag();
        $logData        = [];
        if(!$result) {
            $errMsg     = '同步失败';
            $LogCtl->logAdd(LogCtl::AUTH_REQUEST_ASYNC, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '同步成功';
        $LogCtl->logAdd(LogCtl::AUTH_REQUEST_ASYNC, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /front/role/list
     * @auth frontRoleList
     * @flag REQ_LOG=0
     * @req Srv.Apps.Proto.DTO.Auth.FrontRoleInfoListDTO
     * @rsp Srv.Apps.Proto.VO.Auth.FrontRoleInfoListVO
     * 前端角色列表
     */
    public function frontRoleList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass): bool
    {
        if (!($DTOClass instanceof FrontRoleInfoListDTO) || !($VOClass instanceof FrontRoleInfoListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page                   = new Page();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = AuthCtl::tag()->getFrontRoleInfoList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /front/role/info
     * @auth frontRoleInfo
     * @flag REQ_LOG=0
     * @req Srv.Apps.Proto.DTO.Auth.FrontRoleInfoDTO
     * @rsp Srv.Apps.Proto.VO.Auth.FrontRoleInfoVO
     * 前端角色信息
     */
    public function getFrontRoleInfo(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($RouteConfData);
        if(!($DTOClass instanceof FrontRoleInfoDTO) || !($VOClass instanceof FrontRoleInfoVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $roleId         = $DTOClass->getRoleId();
        $VOClass        = AuthCtl::tag()->getFontRoleInfo($roleId);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /front/role/add
     * @auth frontRoleAdd
     * @req Srv.Apps.Proto.DTO.Auth.FrontRoleAddDTO
     * 前端角色新增
     */
    public function frontRoleAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof FrontRoleAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $roleId         = AuthCtl::tag()->frontRoleAdd($setData);
        $LogCtl         = LogCtl::tag();
        $logData        = $setData;
        if($roleId < 1){
            $errMsg     = '前端角色新增失败';
            $LogCtl->logAdd(LogCtl::AUTH_ROLE_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '前端角色新增成功';
        $LogCtl->logAdd(LogCtl::AUTH_ROLE_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /front/role/edit
     * @auth frontRoleEdit
     * @req Srv.Apps.Proto.DTO.Auth.FrontRoleEditDTO
     * 前端角色编辑
     */
    public function frontRoleEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof FrontRoleEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $roleId         = intval($DTOClass->getRoleId());
        $roleInfoSrc    = AuthCtl::tag()->getFrontRoleInfoSrc($roleId);
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $result         = AuthCtl::tag()->frontRoleEdit($roleId, $setData);
        $LogCtl         = LogCtl::tag();
        $logData        = ['new' => $setData, 'old' => $roleInfoSrc];
        if(!$result) {
            $errMsg     = '前端角色编辑失败';
            $LogCtl->logAdd(LogCtl::AUTH_ROLE_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '前端角色编辑成功';
        $LogCtl->logAdd(LogCtl::AUTH_ROLE_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}